package com.ltg.topactivity;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.util.Log;
import android.widget.Toast;

import com.ltg.aidl.TestAidl;

public class TopService extends Service {
    private static int count = 0;
    public static final String ACTION_CLOSE = "com.ltg.topactivity.CLOSE";

    private CloseBroadCast close = new CloseBroadCast();

    public TopService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter(ACTION_CLOSE);
        registerReceiver(close, filter);
    }

    @Override
    public IBinder onBind(Intent intent) {
        onHandleIntent(intent);
        return new TestBundle();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        onHandleIntent(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("receiver", "unregister..............");
        unregisterReceiver(close);
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancelAll();
    }

    protected void onHandleIntent(Intent intent) {
        if (ACTION_CLOSE.equals(intent.getAction())) {
            stopSelf();
            return;
        }
        count++;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        updateBuilder(builder, intent);
        Notification build = builder.build();
        build.flags |= Notification.FLAG_NO_CLEAR;
        nm.notify(1, build);
    }

    private void updateBuilder(NotificationCompat.Builder builder, Intent intent) {
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        RunningTaskInfo runningTaskInfo = am.getRunningTasks(1).get(0);
        ComponentName topActivity = runningTaskInfo.topActivity;
        PackageManager pm = getPackageManager();

        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(false);
        builder.setTicker("ticker");
        builder.setSmallIcon(android.R.drawable.btn_star);
        try {
            String packageName = topActivity.getPackageName();
            ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_ACTIVITIES);
            builder.setLargeIcon(((BitmapDrawable) applicationInfo.loadIcon(pm)).getBitmap());
            builder.setContentTitle(pm.getApplicationLabel(applicationInfo));
            builder.setContentText("下滑显示更多");
            BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
            String className = topActivity.getClassName();
            if (className.startsWith(packageName)) {
                className = className.substring(packageName.length());
            }
            bigStyle.bigText(packageName + "\n" + className);
            builder.setStyle(bigStyle);
            Intent close = new Intent(ACTION_CLOSE);
//            close.setClass(this, CloseBroadCast.class);
            builder.addAction(android.R.drawable.btn_dialog, "关闭", PendingIntent.getBroadcast(this, 4, close, 0));
            builder.addAction(android.R.drawable.ic_dialog_info, "更新" + String.valueOf(count), PendingIntent.getService(this, 1, intent, 0));
        } catch (NameNotFoundException e) {
        }
    }

    private class TestBundle extends TestAidl.Stub {

        @Override
        public void doSomething() throws RemoteException {
            Log.e("bind", "do some thing in pid:" + Process.myPid());
        }

    }

    private class CloseBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("receiver", "received:" + intent);
            stopSelf();
        }

    }

}
